<?php 
class product_models_admin extends models{
   
    public function get_month_year_create($limit=''){  
    	if($limit) $limit=" LIMIT $limit";  	
		$get= $this->db->query("SELECT DISTINCT DATE_FORMAT(DATE( FROM_UNIXTIME( prd_date_public ) ),'%Y%m' )AS YearMonth,
		    	DATE_FORMAT(DATE( FROM_UNIXTIME( prd_date_public ) ),'%m' )AS month,
		    	DATE_FORMAT(DATE( FROM_UNIXTIME( prd_date_public ) ),'%Y' )AS year
				from product  $limit");
		return $get->result();
    
    }
    public function get_list_product($filter=array(),$sort=array(),$paging=array()){
    	$limit=$paging['number_affiche'];
    	$current=$paging['page_current']*$limit;
    	$where='';
    	$orderBy='';
    	$condition=array();
    	foreach ($filter as $nameFilter=>$valueFilter)
    		if(!empty($valueFilter)){
    			$where=' WHERE ';
    			break;
    		}
    	if(!empty($sort))
    	foreach ($sort as $sortName=>$valueSort)
    		$orderBy="ORDER BY $sortName $valueSort";
    		
    	if($where){
    		if($filter['category']){
    			$condition[]='product_cat.id_category='.$filter['category'];
    		}
    		if($filter['date']){
    			$condition[]="DATE_FORMAT(DATE( FROM_UNIXTIME(product.prd_date_public ) ),'%Y%m' )='{$filter['date']}'";
    		}
    		if(!empty($condition))$where.= implode(' AND ',$condition);
    	}
    	$sql="SELECT DISTINCT product_id 
				FROM product 
				LEFT JOIN product_cat ON product.product_info_id=product_cat.product_info_id 
				LEFT JOIN product_info ON product.product_info_id=product_info.prd_id 
		    	$where 
		    	$orderBy
		    	LIMIT $current,$limit
    		";
		$get= $this->db->query($sql);
		return $get->result();
    
    }
    public function get_total_product($active=FALSE){
    	if($active) $active=" WHERE prd_active=1";
    	else $active='';
    	$get= $this->db->query("SELECT count(*) as numProduct FROM product $active");
    	$row=$get->result();
    	
    	return $row[0]->numProduct;
    }
    public function deactive($id){
    	$this->db->update('product', array('prd_active'=>0), "product_id = $id");
    }
    public function reactive($id){
    	$this->db->update('product', array('prd_active'=>1), "product_id = $id");
    }
}